<template>
  <div>
    <el-steps :active="1" style="margin-top: 30px">
      <el-step title="添加课程信息" icon="el-icon-edit"></el-step>
      <el-step title="添加课程大纲信息" icon="el-icon-upload"></el-step>
      <el-step title="发布课程" icon="el-icon-picture"></el-step>
    </el-steps>
    <el-form ref="form" :model="courseForm" label-width="120px">
      <el-form-item label="课程标题">
        <el-input v-model="courseForm.title" placeholder="示例：机器学习项目课：从入门到入土。专业名注意大小写" />
      </el-form-item>
      <el-form-item label="课程分类">
        <el-select v-model="courseForm.subjectParentId" placeholder="一级分类" style="margin-right: 20px" @change="getTwo(courseForm.subjectParentId)">
          <el-option :label="l1.title" :value="l1.id"  v-for="(l1) in level1" :key="l1.id" />
        </el-select>
        <el-select v-model="courseForm.subjectId" placeholder="二级分类">
          <el-option :label="l2.title" :value="l2.id" v-for="(l2) in level2" :key="l2.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="课程讲师">
        <el-select v-model="courseForm.teacherId" placeholder="请选择课程讲师" style="margin-right: 20px">
          <el-option :label="tea.name" :value="tea.id" v-for="(tea) in teachers" :key="tea.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number size="small" v-model="courseForm.lessonNum" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="课程简介" >
        <quill-editor
          v-model="courseForm.description"/>
      </el-form-item>
      <el-form-item label="课程封面" >
        <el-upload
          :headers="tokenUrl"
          class="avatar-uploader"
          :action="BASE_API+'/course/uploadCover'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="courseForm.cover" :src="courseForm.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number size="small" v-model="courseForm.price"  controls-position="right" :min="0"></el-input-number>元
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="next">保存并下一步</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>

import {getToken} from "@/utils/auth";
import {saveOrupdate, showCourse, showLevel1, showLevel2, showTeacher} from "@/api/course";


export default {
  name: "save",
  data() {
    return {
      BASE_API: process.env.BASE_API,
      teachers:[],
      level1:[],
      level2:[],
      courseForm:{
        id:'',
        teacherId:'',
        subjectParentId:'',
        subjectId:'',
        title:'',
        price:0,
        description:'',
        lessonNum: 1,
        cover:'https://jf3q.oss-cn-beijing.aliyuncs.com/course/default.gif'
      },
      tokenUrl:{
        'Authorization':'Bearer '+getToken()
      },
    };
  },
  created() {
    this.courseForm.id=this.$route.query.cId
  },
  mounted() {
    this.show();
    if(this.courseForm.id!=null){
      this.courseOne();
    }
  },
  methods: {
    courseOne(){
      showCourse(this.courseForm.id).then(res =>{
        this.getTwo(res.data.subjectParentId)
        this.courseForm=res.data
      })
    },
    getTwo(spId){
      this.courseForm.subjectId=''
      showLevel2(spId).then(res =>{
        this.level2=res.data;
      })
    },
    show(){
      showTeacher().then((res)=>{
        this.teachers=res.data;
      })
      showLevel1().then((res)=>{
        this.level1=res.data;
      })
    },
    resetForm(){
      this.courseForm={
        id:'',
        teacherId:'',
        subjectParentId:'',
        subjectId:'',
        title:'',
        price:0,
        lessonNum: 1,
        cover:''
      }
    },
    handleAvatarSuccess(res,file){
      this.courseForm.cover=res.data
    },
    next() {
      saveOrupdate(this.courseForm).then(res =>{
        this.$message.success("操作成功")
        this.$router.push({path:"/course/chapter",query:{cId:res.data}})//传课程id

      })
    }
  }
}
</script>



<style >
.ql-editor{
  height:200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 400px;
  height: 200px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 400px;
  height: 200px;
  display: block;
}
</style>
